import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { JVxeTypes, JVxeColumn } from '/@/components/jeecg/JVxeTable/types'

import { dateUtil } from '/@/utils/dateUtil';
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '班次编码',
    align: "center",
    sorter: true,
    dataIndex: 'shiftCoding'
  },
  {
    title: '班次名称',
    align: "center",
    width: 100,
    dataIndex: 'flightName'
  },
  {
    title: '应出勤天数',
    align: "center",
    width: 100,
    dataIndex: 'daysAttendanceDue'
  },
  {
    title: '状态',
    align: "center",
    sorter: true,
    width: 80,
    dataIndex: 'shiftManagementState',
    customRender: ({ text }) => text == 1 ? '有效' : '无效',
  },
  {
    title: '备注',
    align: "center",
    dataIndex: 'remark'
  },
  {
    title: '第三方编码',
    align: "center",
    dataIndex: 'thirdPartyCode',
    ifShow: false
  }
];
//查询数据
export const searchFormSchema: FormSchema[] = [

  {
    label: "班次编码",
    field: "shiftCoding",
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: "班次名称",
    field: "flightName",
    component: 'Input',
    colProps: { span: 6 },
  },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '班次编码',
    field: 'shiftCoding',
    component: 'Input',
    rules: [
      {
        required: true,
        message: '班次编码必填',
        trigger: 'blur',
      },
    ]
  },
  {
    label: '班次名称',
    field: 'flightName',
    component: 'Input',
    rules: [
      {
        required: true,
        message: '班次名称必填',
        trigger: 'blur',
      },
    ]
  },
  {
    label: '应出勤天数',
    field: 'daysAttendanceDue',
    component: 'InputNumber',
  },
  {
    label: '状态',
    field: 'shiftManagementState',
    component: 'Select',
    defaultValue: 1,
    componentProps: {
      options: [{ value: 1, label: '有效' }, { value: 0, label: '无效' }],
      stringToNumber: true,
    }
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
    colProps: {
      span: 12
    }
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false
  },
];
export const computeShiftHours = function (row: any): string | null {

  if (row.entryTime && row.postSetTime) {

    // 夜班需要跨天
    const start = dateUtil('2020-01-01 ' + row.entryTime)
    const end = dateUtil('2020-01-02 ' + row.postSetTime)

    const hours = end.diff(start, 'hour', true)
    if (hours < 24 && hours > 8) {
      row.hourage = null
      return '单个班次时长不能超过8小时'
    }
    else if (hours > 24) {
      row.hourage = (hours - 24).toFixed(1)
    }
    else {
      row.hourage = hours.toFixed(1)
    }
  }

  return null
}
//子表单数据
//子表表格配置
export const basShiftManagementItemColumns: JVxeColumn[] = [
  {
    title: '序号',
    key: 'serialNumber',
    type: JVxeTypes.input,
    width: "100px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '时段',
    key: 'timeFrame',
    type: JVxeTypes.select,
    width: "100px",
    options: [{ value: 1, label: '上午班' }, { value: 2, label: '下午班' }, { value: 3, label: '晚上班' }],
    placeholder: '请选择${title}',
    defaultValue: 1,
  },
  {
    title: '进入时间',
    key: 'entryTime',
    type: JVxeTypes.time,
    width: "100px",
    placeholder: '请选择${title}',
    defaultValue: null,
    validateRules: [
      {
        handler({ cellValue, row }, callback, target) {
          const messag = computeShiftHours(row)
          if (messag) {
            callback(false, messag)
          } else {
            callback(true);
          }
        }
      }
    ],
  },
  {
    title: '退出时间',
    key: 'postSetTime',
    type: JVxeTypes.time,
    width: "100px",
    placeholder: '请选择${title}',
    defaultValue: null,
    validateRules: [
      {
        handler({ cellValue, row }, callback, target) {
          const messag = computeShiftHours(row)
          if (messag) {
            callback(false, messag)
          } else {
            callback(true);
          }
        }
      }
    ],
  },
  {
    title: '小时数',
    key: 'hourage',
    type: JVxeTypes.normal,
    width: "80px",
    defaultValue: '',
    disabled: true
  },
  {
    title: '弹性时间',
    key: 'flexTime',
    type: JVxeTypes.select,
    width: "100px",
    options: [{ value: 1, label: '是' }, { value: 0, label: '否' }],
    placeholder: '请输入${title}',
    defaultValue: 1,
  },
  {
    title: '跨天是否以进入时间为准',
    key: 'whetherBasedEntryTime',
    type: JVxeTypes.select,
    width: "200px",
    placeholder: '请输入${title}',
    options: [{ value: 1, label: '是' }, { value: 0, label: '否' }],
    defaultValue: 1,
  },
  {
    title: '班次管理id',
    key: 'shiftManagementId',
    type: JVxeTypes.hidden,
    width: "120px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '第三方编码',
    key: 'thirdPartyCode',
    type: JVxeTypes.hidden,
    width: "120px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
  {
    title: '备注',
    key: 'remark',
    type: JVxeTypes.input,
    width: "200px",
    placeholder: '请输入${title}',
    defaultValue: '',
  },
]


/**
* 流程表单调用这个方法获取formSchema
* @param param
*/
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
